package com.laotou99.bookManage.controller;

import java.util.List;

import javax.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.laotou99.bookManage.IDao.BookNameMapper;
import com.laotou99.bookManage.IDao.custNameMapper;
import com.laotou99.bookManage.domain.BookName;
import com.laotou99.bookManage.domain.custName;


@Controller
public class BookController {
	
	@Resource
	private BookNameMapper bookNameMapper;
	
	@Resource 
	private custNameMapper custNameMapper1;

	private static final Log logger = LogFactory.getLog(BookController.class);
	
	/**
	 * 显示所有图书
	 * */
	@RequestMapping(value="/showbook")
	public String showBook(Model model)
	{
		logger.info("showbook");
		List<BookName> books = bookNameMapper.getAllBooks();
		model.addAttribute("books", books);
		
		Subject currentuser = SecurityUtils.getSubject();
		Object abc = currentuser.getPrincipal();
		System.out.println("currentuser->"+currentuser.toString());
		System.out.println("abc->"+abc.toString());
//		System.out.println(books.size());
//		System.out.println(books.get(0).getName());
//		BookName bn1 = new BookName();
//		bn1.setName("abc");
//		model.addAttribute("bookname", bn1);
		return "showAllBooks";
	}
	
	/**
	 * 登入
	 * */
	@RequestMapping(value="/login1",method=RequestMethod.POST)
	public String execute(HttpServletRequest request,
			HttpServletResponse response,
			String username,
			String password)
	{
		logger.info("String execute->login1");
		UsernamePasswordToken token = 
				new UsernamePasswordToken(username, password);
		
		token.setRememberMe(false);
		
		Subject subject = SecurityUtils.getSubject();
		
		try {
			subject.login(token);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "redirect:/showbook";
	}
	
	/**
	 * logout
	 * */
	@RequestMapping(value="/logout")
	public String test1(){
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "logoin";
	}
	
	/**
	 * logoin
	 * */
	@RequestMapping(value="/logoin")
	public String logoin(){
		logger.info("String logoin->logoin");
		return "logoin";
	}
	
	/**
	 *会员管理 
	 * */
	@RequestMapping(value="/memberManage")
	public String memberManage(Model model){
		List<custName> cn1 = custNameMapper1.selectAllcustName();
		
		model.addAttribute("custNames", cn1);
		return "membermanage";
	}
	
	//updateBook/${BookName.id}
	@RequestMapping(value="/updateBook/{id}")
	public String updateBook(Model model,@PathVariable int id){
		BookName bookName = bookNameMapper.selectByPrimaryKey(id);
		model.addAttribute("bookName", bookName);
		return "editBookForm";
	}
	
	//deleteBook/${BookName.id}
	@RequestMapping(value="/deleteBook/{id}")
	public String deleteBook(@PathVariable int id)
	{
		System.out.println("deleteBook");
		bookNameMapper.deleteByPrimaryKey(id);
		return "redirect:/showbook";
	}
	
	
	//updateSaveBook
	@RequestMapping(value="/updateSaveBook",method=RequestMethod.POST)
	public String updateSaveBook(@ModelAttribute BookName bookName,
			BindingResult bindingResult,Model model)
	{
		if(bindingResult.hasErrors()){
			FieldError fieldError = bindingResult.getFieldError();
			logger.info("Code:"+fieldError.getCode()+",field:"+fieldError.getField());
			return "editBookForm";
		}
		System.out.println("bookName.getId()->"+bookName.getId());
		bookNameMapper.updateByPrimaryKeySelective(bookName);
		return "redirect:/showbook";
	}
	
	/**
	 * 添加新书本
	 * */
	@RequestMapping(value="/bookAdd")
	public String bookAdd(Model model){
		System.out.println("bookAdd");		
		logger.info("bookAdd");
		model.addAttribute("bookName", new BookName());
		return "bookAddForm";
	}
	
	//saveBook
	@RequestMapping(value="/saveBook")
	public String saveBook(@ModelAttribute BookName bookName){
		logger.info("saveBook");
		System.out.println("saveBook");
		bookNameMapper.insert(bookName);
		return "redirect:/showbook";
	}	 
}
